﻿using Glorysoft.EAP.Core.Enums;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using FluentScheduler;
using Glorysoft.EAP.Core;
using Glorysoft.Auto.Xfw;
using Serilog.Core;
using Glorysoft.EAP.RC.EAPClient;
using System.IO;
using System.Linq;
using System.Drawing;
using Glorysoft.EAP.Core.Entities.DB;
using Glorysoft.EAP.Core.Entities.DB.Service;
using Org.BouncyCastle.Ocsp;
using System.Security.Cryptography;
using Glorysoft.EAP.Contracts.MESClient;
using Glorysoft.EAP.RC.MES;

namespace Glorysoft.EAP.EAPwell
{
    /// <summary>
    /// 监控monitor文件计划调度器固定计划调度器
    /// </summary>
    public class RetryTrackOutLot : IJob
    {
        MESService MESService = null;
        public static Logger TrackoutLogger = XfwContext.LogManager.Create("TrackoutLogger");

        public void Execute()
        {
            List<string> lotIdList = Dict.Current.TrackOutLotQueue.Keys.ToList();
            List<LotInfo> lotInfoList = Dict.Current.TrackOutLotQueue.Values.ToList();
            for (int i = 0; i < lotInfoList.Count; i++)
            {
                if (lotInfoList[i].trackOutRetryCount > CItems.TrackOutCount)
                {
                    TrackoutLogger.Information($"*************批次出站重试{lotInfoList[i].lotId}第{lotInfoList[i].trackOutRetryCount}次,移除出站*********");
                    Dict.Current.TrackOutLotQueue.Remove(lotIdList[i]);
                }
            }
            //STEP 1 >>> 查找所有待出站批次
            foreach (var item in Dict.Current.TrackOutLotQueue)
            {
                if (item.Value.monitorLotFlag)
                {
                    continue;
                }
                var wafer = item.Value.waferList.Find(wafer => wafer.processFlag != "Y");
                //如果批次内存在没测试的wafer就不出站
                if (wafer != null)
                {
                    TrackoutLogger.Information($"*************批次{item.Key}未作业结束，跳过出站，未测试waferID{wafer.waferId}*********");
                    if ((DateTime.Now.AddDays(-3) > item.Value.creatTime))
                    {
                        item.Value.trackOutRetryCount = CItems.TrackOutCount + 1;
                        TrackoutLogger.Information($"*************批次{item.Key} 状态为inProcessing，入站时间为: {item.Value.creatTime}超过3天，停止检测出站*********");
                    }
                }
                else
                {
                    TrackoutLogger.Information($"*************批次{item.Key}*********");
                    MESService = new MESService();
                    var eqpType = Dict.Current.TesterType;
                    if (Dict.Current.TesterType == OCRType.Raytrex_E4980AOCR.ToString())
                    {
                        MESService.TrackOutLotLuDing(item.Value);
                    }
                    else
                    {
                        MESService.TrackOutLotRetry(item.Value);
                    }

                    TrackoutLogger.Information($"*************批次出站重试{item.Key}第{item.Value.trackOutRetryCount}次结束,设备类型{eqpType}*********");
                }
            }

        }
    }
}
